/*
 * @Description: 
 * @Author: 马爽
 * @Date: 2021-03-09 08:20:46
 * @LastEditTime: 2021-03-10 11:29:58
 * @LastEditors: 马爽
 */


import { FC } from 'react'
import { useDispatch } from 'umi';
import { Button, Select, Form} from 'antd';
import { View_authority } from '@/interface/user';

const { Option } = Select;

const layout = {
  labelCol: { span: 8 },
  wrapperCol: { span: 16 },
};

const tailLayout = {
  wrapperCol: { offset: 1, span: 16 },
};

interface Iprops {
    viewAuthorityList: View_authority[]
}

const AddauthorityView:FC<Iprops> = ({viewAuthorityList}) => {
    const dispatch = useDispatch();
    
    const [form] = Form.useForm();

    const onReset = () => {
        form.resetFields();
    };

    function handleChange(value: any) {
        console.log(`selected ${value}`);
    }
        
    const onFinish = (values: any) => {
        // console.log('Success:', values);
        let list = viewAuthorityList.filter(item => item.view_authority_text === values.view_authority_text)[0];

        //是否成功 
        dispatch({
            type:'userManage/authorityView',
            payload:{...values,'view_id':list.view_id}
        })
    };
    
    return (
        <div className='addbox'>
          <div className='btn'>
            <Button>添加视图接口权限</Button>
          </div>
          <Form
            form={form}
            {...layout}
            name="basic"
            onFinish={onFinish}
          >
            <Form.Item
              name="view_authority_text"
              rules={[{ required: true, message: '已有视图' }]}
            >

              <Select style={{ width: 124 }} onChange={handleChange} placeholder={'请选择已有视图'}>
                {
                  viewAuthorityList.map((item,index) => {
                    return (
                      <Option value={item.view_authority_text} key={index}>{item.view_authority_text}</Option>
                    )
                  })
                }
              </Select>
            </Form.Item>

            <Form.Item {...tailLayout}>
              <Button type="primary" htmlType="submit">
                确定
              </Button>
              <Button htmlType="button" onClick={onReset}>
                重置
              </Button>
            </Form.Item>
          </Form>
        </div>
    )
}

export default AddauthorityView;
